package cn.edu.henu.team.ui_test;

import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bin.david.form.core.SmartTable;
import com.bin.david.form.core.TableConfig;
import com.bin.david.form.data.CellInfo;
import com.bin.david.form.data.format.bg.BaseCellBackgroundFormat;
import com.bin.david.form.data.style.FontStyle;

import java.util.ArrayList;
import java.util.List;

import androidx.core.content.ContextCompat;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;
import cn.edu.henu.team.R;
import cn.edu.henu.team.base.BaseActivity;
import cn.edu.henu.team.bean.diagnosticLog;
import cn.edu.henu.team.table.testResult;

public class FindSysLogActivity extends BaseActivity {
    private ImageView back;
    private String objectId;
    private TextView time;
    private TextView content;
    private TextView answer;
    private SmartTable table;

    @Override
    protected int initLayout() {
        return R.layout.activity_sys_log;
    }

    @Override
    protected void initView() {
        objectId = getIntent().getStringExtra("objectId");
        back =  findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        time = findViewById(R.id.time);
        content = findViewById(R.id.pingYu);
        answer = findViewById(R.id.result);
        table = findViewById(R.id.table);
    }

    @Override
    protected void initData() {
        List<testResult> list = new ArrayList<>();
        BmobQuery<diagnosticLog> query = new BmobQuery<diagnosticLog>();
        query.getObject(objectId, new QueryListener<diagnosticLog>() {
            @Override
            public void done(diagnosticLog diagnosticLog, BmobException e) {
                time.setText(diagnosticLog.getCreatedAt().toString());
                content.setText(diagnosticLog.getContent().toString());
                answer.setText(diagnosticLog.getAnswer().toString());

                //加载表格数据
                if(diagnosticLog.getFindDN1() == 1){
                    list.add(new testResult("龋病", String.format("%.2f", diagnosticLog.getP1()) + "%", "比较严重,建议就医"));
                }else{
                    list.add(new testResult("龋病", String.format("%.2f", diagnosticLog.getP1()) + "%", "一般情况，可自行用药"));
                }

                if(diagnosticLog.getFindDN2() == 1){
                    list.add(new testResult("牙髓炎", String.format("%.2f", diagnosticLog.getP2()) + "%", "比较严重,建议就医"));
                }else{
                    list.add(new testResult("牙髓炎", String.format("%.2f", diagnosticLog.getP2()) + "%", "一般情况，可自行用药"));
                }

                if(diagnosticLog.getFindDN3() == 1){
                    list.add(new testResult("根尖周炎", String.format("%.2f", diagnosticLog.getP3()) + "%", "比较严重,建议就医"));
                }else{
                    list.add(new testResult("根尖周炎", String.format("%.2f", diagnosticLog.getP3()) + "%", "一般情况，可自行用药"));
                }

                table.setData(list);
                table.setZoom(true);
                table.getConfig().setShowTableTitle(false);
                table.getConfig().setShowXSequence(false);
                table.getConfig().setShowYSequence(false);
                table.getConfig().setMinTableWidth(1024);       //设置表格最小宽度
                FontStyle style = new FontStyle();
                style.setTextSize(35);
                style.setTextColor(Color.BLUE);
                table.getConfig().setContentStyle(style);       //设置表格主题字体样式
                table.getConfig().setColumnTitleStyle(style);   //设置表格标题字体样式
                table.getConfig().setContentCellBackgroundFormat(new BaseCellBackgroundFormat<CellInfo>() {     //设置隔行变色
                    @Override
                    public int getBackGroundColor(CellInfo cellInfo) {
                        if (cellInfo.row % 2 == 1) {
                            return ContextCompat.getColor(getApplicationContext(), R.color.tableBackground);
                        } else {
                            return TableConfig.INVALID_COLOR;
                        }
                    }
                });
            }
        });
    }
}